package xyz.jangle.base;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
/**
 * 树映射 TreeMap demo
 * 
 * @author Administrator
 *
 */
public class Example13_9 {

	public static void main(String[] args) {
		Student3 s1 = new Student3("A",60);
		Student3 s2 = new Student3("B",80);
		Student3 s3 = new Student3("C",70);
		Student3 s4 = new Student3("D",90);
//		Student3 s5 = new Student3("E",60);
		Map<Student3Key,Student3> map = new TreeMap<Student3Key,Student3>();
		map.put(s1.getMath(), s1);
//		map.put(s5.getMath(), s5);
		map.put(s2.getMath(), s2);
		map.put(s3.getMath(), s3);
		map.put(s4.getMath(), s4);
		System.out.println(map.get(s1.getMath()));	//get方法通过compareTo方法返回0来确定为相同。

//		for(Student3Key key:map.keySet()) {
//			System.out.println(map.get(key).getName()+","+map.get(key).getMath());
//		}
		System.out.println("--------------");
		Iterator<Student3> iterator = map.values().iterator();
		while(iterator.hasNext()) {
			Student3 next = iterator.next();
			System.out.println(next.getName()+","+next.getMath().getMath());
		}
	}

}

class Student3 {
	
	private String name;
	private Student3Key key;
	public Student3(String name, Integer math) {
		super();
		this.name = name;
		this.key = new Student3Key(math);
	}
	public String getName() {
		return name;
	}
	public Student3Key getMath() {
		return key;
	}
	
}

class Student3Key implements Comparable<Student3Key>{
	private Integer math;

	public Student3Key(Integer math) {
		super();
		this.math = math;
	}

	public Integer getMath() {
		return math;
	}

	@Override
	public int compareTo(Student3Key o) {
//		if(this.math - o.math==0)
//			return 1;					//如果将相等的值返回为1或者-1，则map遇到相等的值不会被覆盖。（但因此map的get方法将无法使用）
//		else
			return this.math - o.math;	// 返回0代表相等（get方法通过key相等来获取value值）
	}
	
	
}
